package com.book.util;

import cn.hutool.core.io.FileUtil;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author SuperYu  2024/7/29
 */
public class Generator {
    // 1：备份原有代码
    public static void backUp() {
        String mapperLocation = "book-mall-mapper/src/main/java/com/book/mapper";
        File mapperFolder = new File(mapperLocation);

        File[] files = mapperFolder.listFiles();
        if (files != null && files.length != 0) {
            // 备份的文件夹
            File backupFolder = new File("book-mall-mapper/src/main/java/com/book/backup");
            // 复制操作：使用hutool工具里的文件复制
            FileUtil.copyContent(mapperFolder,backupFolder, false);

            System.out.println("----backup ok----");
        }

    }


    // 2：逆向工程生成我们的代码：
    public static void create() {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = null;

        try {
            config = cp.parseConfiguration(
                    // Generator.class.getResourceAsStream("generatorConfig.xml"));
                    Generator.class.getClassLoader().getResourceAsStream("generatorConfig.xml"));

            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMLParserException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }
    }


    // 3: 恢复备份的代码：
    public static void restore(){
        // 备份文件夹
        File backupFolder = new File("book-mall-mapper/src/main/java/com/book/backup");
        // 得到备份目录里的内容
        File[] files = backupFolder.listFiles();
        if(files != null && files.length != 0){
            // 还原数据的操作
            String mapperLocation = "book-mall-mapper/src/main/java/com/book/mapper";
            File mapperFolder = new File(mapperLocation);
            FileUtil.copyContent(backupFolder, mapperFolder,true);
            // 完成恢复后，要把备份文件夹删除
            boolean f = FileUtil.del(backupFolder);
            System.out.println("--- 完成恢复后，备份文件夹是否删除："+f);
        }


    }


    public static void main(String[] args) {
        // 1: 先备份原有代码
        backUp();

        // 2：生成代码
        create();

        // 3：把备份的代码恢复
        restore();

        System.out.println("----end----");
    }
}

